package com.engine.login.web;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.OnLineMonitor;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaLoginWeb {
    /**
     * @Description: 获取汇总表任务数据
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/10
     */
    @GET
    @Path("/ssoLogin")
    @Produces({MediaType.TEXT_PLAIN})
    public void ssoLogin(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        String  tourl = "";
        String appid = Util.null2String(request.getParameter("appid"));
        String loginid = Util.null2String(request.getParameter("loginid"));
        baseBean.writeLog( ">>>>>ssoLogin>>>loginid=" + loginid );
        try {
            if(loginid.equals("sysadmin")){
                rs.execute("select * from hrmresourcemanager where loginid='"+loginid+"' ");
            }else{
                rs.execute("select * from HrmResource where loginid='"+loginid+"' or workcode='"+loginid+"'");
            }
            if(rs.next()) {
                User user = new User();
                user.setUid(rs.getInt("id"));
                user.setLoginid(rs.getString("loginid"));
                user.setFirstname(rs.getString("firstname"));
                user.setLastname(rs.getString("lastname"));
                user.setAliasname(rs.getString("aliasname"));
                user.setTitle(rs.getString("title"));
                user.setTitlelocation(rs.getString("titlelocation"));
                user.setSex(rs.getString("sex"));
                user.setPwd(rs.getString("password"));
                String languageidweaver = rs.getString("systemlanguage");
                user.setLanguage(Util.getIntValue(languageidweaver, 0));
                user.setTelephone(rs.getString("telephone"));
                user.setMobile(rs.getString("mobile"));
                user.setMobilecall(rs.getString("mobilecall"));
                user.setEmail(rs.getString("email"));
                user.setCountryid(rs.getString("countryid"));
                user.setLocationid(rs.getString("locationid"));
                user.setResourcetype(rs.getString("resourcetype"));
                user.setStartdate(rs.getString("startdate"));
                user.setEnddate(rs.getString("enddate"));
                user.setContractdate(rs.getString("contractdate"));
                user.setJobtitle(rs.getString("jobtitle"));
                user.setJobgroup(rs.getString("jobgroup"));
                user.setJobactivity(rs.getString("jobactivity"));
                user.setJoblevel(rs.getString("joblevel"));
                user.setSeclevel(rs.getString("seclevel"));
                user.setUserDepartment(Util.getIntValue(rs.getString("departmentid"), 0));
                user.setUserSubCompany1(Util.getIntValue(rs.getString("subcompanyid1"), 0));
                user.setUserSubCompany2(Util.getIntValue(rs.getString("subcompanyid2"), 0));
                user.setUserSubCompany3(Util.getIntValue(rs.getString("subcompanyid3"), 0));
                user.setUserSubCompany4(Util.getIntValue(rs.getString("subcompanyid4"), 0));
                user.setManagerid(rs.getString("managerid"));
                user.setAssistantid(rs.getString("assistantid"));
                user.setPurchaselimit(rs.getString("purchaselimit"));
                user.setCurrencyid(rs.getString("currencyid"));
                user.setLastlogindate(rs.getString("currentdate"));
                user.setLogintype("1");
                user.setAccount(rs.getString("account"));
                user.setLoginip(request.getRemoteAddr());
                request.getSession(true).setMaxInactiveInterval(60 * 60 * 24);
                request.getSession(true).setAttribute("weaver_user@bean", user);
                request.getSession(true).setAttribute("moniter", new OnLineMonitor("" + user.getUID(), user.getLoginip()));
                Util.setCookie(response, "loginfileweaver", "/main.jsp", 172800);
                Util.setCookie(response, "loginidweaver", "" + user.getUID(), 172800);
                Util.setCookie(response, "languageidweaver", languageidweaver, 172800);
                //用户的登录后的页面
                weaver.systeminfo.template.UserTemplate ut = new weaver.systeminfo.template.UserTemplate();
                ut.getTemplateByUID(user.getUID(), user.getUserSubCompany1());
                tourl = "/spa/workflow/static4form/index.html#/main/workflow/req?iscreate=1&workflowid=10";
            }
             response.sendRedirect(tourl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
